<template>
	<view><button open-type="getUserInfo" @getuserinfo="wxLogin">点我登录</button></view>
</template>

<script>
	export default {
		methods: {
			wxLogin(userInfoRes) {
				// 获取用户信息
				let {
					encryptedData,
					signature,
					iv,
					rawData
				} = userInfoRes.detail;
				// 获取code
				uni.login({
					success: async loginRes => {
						let code = loginRes.code
						// 发请求
						let data = await this.$request({
							url: '/api/public/v1/users/wxlogin',
							method: 'POST',
							data: {
								encryptedData,
								signature,
								iv,
								rawData,
								code
							}
						});
						// 将token值存储
						uni.setStorageSync('token', data.token)
						// // 存储用户信息
						uni.setStorageSync('userInfo', userInfoRes.detail.userInfo);
						uni.showToast({
							title: '登陆成功',
							success: () => {
								// 成功后返回
								setTimeout(() =>{
									uni.navigateBack()
									},1500)
							}
						});
					}
				});
			}
		}
	};
</script>

<style></style>
